package com.example.ytc.Fragement;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.example.ytc.App;
import com.example.ytc.R;
import com.example.ytc.Yt_Video_player;
import com.example.ytc.clazz.Ivideo;
import com.example.ytc.clazz.Video;
import com.example.ytc.tools.FavoritDAO;
import com.itconnect.inc.adapters.YouTubeAdapter;

public class favorit_fragment extends Fragment {
	ListView lv;
	
	App appState;
	ArrayList<Video> vid = new ArrayList<Video>();

	public favorit_fragment() {
	}

	public static favorit_fragment newInstance(String ytuser) {
		favorit_fragment fragment = new favorit_fragment();
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.fragment_favorits,
				container, false);
		appState = ((App) view.getContext().getApplicationContext());
		lv = (ListView) view.findViewById(R.id.list_favo);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> myAdapter, View myView,
					int myItemInt, long mylng) {
				Intent myIntent = new Intent(getActivity(),
						Yt_Video_player.class);
				appState.setSelected_Video(vid.get(myItemInt));
				// Optional
				getActivity().startActivity(myIntent);
			}
		});
		appState.fav_list = new YouTubeAdapter(view.getContext(), vid);
		lv.setAdapter(appState.fav_list);
		
			getdata(view);
		
		return view;

	}

	private void getdata(View view) {
		// TODO Auto-generated method stub
		//FavoritDAO.clear(view.getContext().getApplicationContext());
		List<Ivideo> lst = null;
		try {
			lst = FavoritDAO.get(view.getContext().getApplicationContext());
		
		for (Ivideo v : lst) {
			vid.add(new Video(v.getYt_id(), v.getTitel(), v.getImage_url(), v
					.getDescription(), v.url_toplay, v.getDuration()));
		}
		appState.fav_list.notifyDataSetChanged();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
